package com.ikkong.biz.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.beetl.sql.ext.jfinal.Trans;

import com.google.common.collect.BiMap;
import com.google.common.collect.Maps;
import com.ikkong.biz.meta.intercept.ParentIntercept;
import com.ikkong.biz.model.Student;
import com.ikkong.biz.service.impl.ClassesServiceImpl;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JStrKit;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.dao.Db;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.toolbox.Func;
import com.ikkong.core.toolbox.Record;
import com.ikkong.system.controller.base.AdminBaseController;
import com.ikkong.biz.model.Classes;
import com.ikkong.biz.model.Parent;
import com.ikkong.biz.model.Student;
import com.ikkong.biz.service.AsyncWeixinService;
import com.ikkong.biz.service.ParentService;
import com.ikkong.biz.service.AsyncWeixinService.OPTYPE;
import com.ikkong.biz.service.impl.AsyncWeixinServiceImpl;
import com.ikkong.biz.service.impl.ParentServiceImpl;
import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;

/**
 * Generated by Blade.
 * 2016-11-17 16:03:13
 */
public class ParentController extends AdminBaseController {
	private static String CODE = "parent";
	private static String PERFIX = "tb_parent";
	private static String LIST_SOURCE = "Parent.list";
	private static String BASE_PATH = "/biz/parent/";
	
	ParentService service = new ParentServiceImpl();
    AsyncWeixinService asyncService = new AsyncWeixinServiceImpl();

	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "parent.html");
	}

	public void add() {
		setAttr("code", CODE);
        Integer stuId = this.getParaToInt("stuId",0);
        System.out.println("1111:"+stuId);
        if (stuId==0) {
            renderText("必须选择一个学生来新增家长!");
            return;
        }
        Student s = Blade.create(Student.class).findById(stuId);
        setAttr("stu_name", s.getName());
        setAttr("stu_id",stuId);
        setAttr("stu_mobile",s.getMobile());
		render(BASE_PATH + "parent_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Parent parent = service.findById(id);
		setAttr("model", JsonKit.toJson(parent));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "parent_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Parent parent = service.findById(id);
		parent.setPositionStr(Func.getDictName(1003, parent.getPosition()));
        Map<Integer,String> classesNames = new ClassesServiceImpl().allClassNames();
        
        parent.setClass_name(classesNames.get(parent.getClass_id()));
		setAttr("model", JsonKit.toJson(parent));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "parent_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE,new ParentIntercept());
		renderJson(grid);
	}

	public void save() {
		Parent parent = mapping(PERFIX, Parent.class);
		Parent db = service.findFirstBy("mobile="+parent.getMobile(), null);
		if(null !=db){
			renderJson(error("手机号已经存在,请重新输入"));
			return;
		}
		Map<String,Object> modelOrMap = Maps.newHashMap();
		modelOrMap.put("mobile", parent.getStu_mobile());
		Student student = Blade.create(Student.class).findFirstBy(" mobile = #{mobile}", modelOrMap);
		parent.setStu_name(student.getName());
		
		Classes classes = Blade.create(Classes.class).findById(student.getClass_id());
		parent.setDept_id(classes.getParent_dept_id());
		parent.setClass_id(student.getClass_id());
		parent.setVersion(0);

		boolean result = service.save(parent);

		if (result) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	@Before(Trans.class)
	public void update() {
		Parent parent = mapping(PERFIX, Parent.class);
		Parent db = service.findFirstBy("mobile="+parent.getMobile()+" and id!="+parent.getId(), null);
		if(null !=db){
			renderJson(error("手机号已经存在,请重新输入"));
			return;
		}
		parent.setVersion(parent.getVersion()+1);
		boolean temp = service.update(parent);

		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
        String[] array = ids.split(",");
        if (cnt > 0) {
            renderJson(success(DEL_SUCCESS_MSG));
            Parent parent= new Parent();
            for (int i = 0; i < array.length; i++) {
                if(JStrKit.isBlank(array[i])){

                }else{
                    parent.setId(Integer.parseInt(array[i]));
                    asyncService.asyncParent(parent, OPTYPE.DELETE);
                }
            }
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}

    public void toImport(){
        setAttr("code", CODE);
        render(BASE_PATH + "parent_import.html");
    }

    public void doImport(){
		List<UploadFile> flist = this.getFiles("/temp", 1024*1024*50);
		if(flist.size()>0){
			UploadFile uf=flist.get(0);
	        String result = service.doImport(uf.getFile());
			renderJson(success(result));
		}

    }
}
